import {
  unix
} from "dayjs";

getApp().authPage.basePage({

  /**
   * 页面的初始数据
   */
  data: {
    isSubmitMore: false,
    // 出价
    formData: {
      projectId: "",
      bidPriceMoney: ""
    },
    moreFormData: {
      "projectId": 0,
      "userName": "",
      "userMobile": ""
    },
    coverImage: 'https://cdn.nlark.com/yuque/0/2024/jpeg/280373/1728909064581-assets/web-upload/dd3d12d5-8320-41f4-9daf-cf50def2e6e9.jpeg', //封面图片
    showPopup: false,
    showMore: false,
    content: null,
  },
  // 前往
  goConnection(e) {
    wx.showLoading({
      title: '请稍后...',
    })
    wx.setStorage({
      key: "detailsItem",
      data: this.data.content,
      success() {
        setTimeout(() => {
          wx.hideLoading();
          wx.navigateTo({
            url: '/pages/leaveMessage/leaveMessage',
          })
        }, 200)
      }
    })
  },
  getData(id) {
    var that = this;
    that.showLoadingIndex = wx.showLoading({
      title: '请稍后...',
    })
    getApp().http.getProjectDetail({
      projectId: id
    }).then(res => {
      console.log(res);
      if (res.errorCode === 401) {
        setTimeout(() => {
          that.getData(id);
          wx.hideLoading(that.showLoadingIndex);
        }, 1500)
      } else {
        wx.hideLoading(that.showLoadingIndex)
        that.setData({
          content: res.data,
        });
      }
    }).catch(err => {
      wx.hideLoading(that.showLoadingIndex)
    })
  },
  onClose() {
    this.setData({
      showPopup: false,
      showMore: false
    })
  },
  setKeyValue(e) {
    var {
      value
    } = e.detail
    var {
      key
    } = getApp().utils.getParameter(e);
    console.log({
      [key]: value
    })
    this.setData({
      [key]: value,
    });
  },
  // 提交更多信息
  submitMore() {
    var that = this;
    let {
      moreFormData
    } = this.data
    let userNameTest = getApp().$verify.verifyTest('isEmpty', moreFormData.userName, '昵称不能为空');
    let userMobileTest = getApp().$verify.verifyTest('phone', moreFormData.userMobile);
    if (!userNameTest.result || !userMobileTest.result) {
      return;
    }
    getApp().http.projectMore(this.data.moreFormData).then(res => {
      if (res.errorCode === 200) {
        wx.showToast({
          title: '提交成功',
          icon: "none",
          success() {
            that.setData({
              isSubmitMore: true,
              showMore: false,
            })
          }
        });


      } else {
        wx.showToast({
          title: '提交失败',
          icon: "error"
        })
      }
    })
  },

  // 出价接口
  projectBid() {
    var newData = this.data.formData;
    var that = this;
    getApp().http.projectBid(newData).then(res => {
      this.setData({
        showPopup: false,
      })
      that.setData({
        showMore: true
      });
      if (res.errorCode === 200) {
        wx.showToast({
          title: '出价成功',
          icon: "success",
        })
      } else {
        wx.showModal({
          title: '出价失败',
          content: res.errorMessage,
          showCancel: false,
        })
      }
    }).catch(err => {
      console.log(err);
    })
  },
  confirmsubmitBid() {
    var that = this;
    if (!this.data.formData.bidPriceMoney) {
      wx.showToast({
        title: '请输入价格',
        icon: "none"
      })
      return;
    }
    wx.showModal({
      title: '温馨提示',
      content: '是否确认出价' + this.data.formData.bidPriceMoney + "万/元?",
      success(res) {
        if (res.confirm) {
          // 调用提交接口
          that.projectBid();
        }
      }
    })
  },


  // 添加收藏
  getMyCollect(e) {
    var that = this;
    var formData = this.data.content.projectCollect;
    let {
      status
    } = e.currentTarget.dataset;
    // 传入 0  取消收藏
    // 传入 1  添加收藏
    if (formData === null) {
      formData = {
        projectId: this.data.content.projectId,
        collectStatusValue: Number(status)
      }
    } else {
      formData.collectStatusValue = Number(status)
    }
    getApp().http.getProjectCollect(formData).then(res => {
      if (res.errorCode === 200) {
        that.setData({
          "content.projectCollect": {
            ...formData,
            ...res.data
          },
        })
        if (formData.collectStatusValue === 1) {
          wx.showToast({
            title: '收藏成功',
            icon: "none",
          })
        } else {
          wx.showToast({
            title: '已取消收藏',
            icon: "none",
          })
        }
      }
      // that.getData(that.data.content.projectId);
    }).catch(err => {
      console.log(err);
    })
  },
  openMore() {
    this.setData({
      showMore: !this.data.showMore
    })
  },
  goBack() {
    let pages = getCurrentPages();
    console.log(pages.length === 1);
    if (pages.length === 1) {
      wx.reLaunch({
        url: '/pages/index/index',
      })
    } else {
      wx.navigateBack();
    }
  },
  openPopup() {
    this.setData({
      showPopup: !this.data.showPopup,
      formData: {
        projectId: this.data.content.projectId,
        bidPriceMoney: ""
      }
    })
  },
  onClickLeft(e) {
    console.log("返回");
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  // 拨打电话
  callPhone(phone) {
    wx.makePhoneCall({
      phoneNumber: (phone),
      success() {
        wx.showToast({
          icon: "none",
          title: '成功',
        })
      },
      fail() {
        wx.showToast({
          icon: "none",
          title: '失败',
        })
      }
    })
  },
  async callSellerPhone() {
    var LoadingIndex1 = wx.showLoading({
      title: '请稍后...',
    })
    const id = this.data.content?.projectLink?.linkToUserId;
    var that = this;
    if (id) {
      let res = await getApp().http.requestLinkInformation({
        linkToUserId: id
      });
      if (res.errorCode === 200) {
        wx.hideLoading(LoadingIndex1);
        wx.showModal({
          content: '是否立即拨打电话联系',
          complete: (resf) => {
            if (resf.confirm) {
              that.callPhone(res.data.mobile);
            }
          }
        })
      } else {
        wx.showToast({
          icon: "none",
          title: '请求号码失败，请联系客服',
        })
      }
    } else {
      wx.showToast({
        title: '失败',
      })
    } 
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const options = getApp().utils.getCurrentPageParam();
    this.getData(options.id);
    this.setData({
      'moreFormData.projectId': options.id,
      menu: getApp().globalData.view.menu
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})